package com.mdhw.system.service.impl;

import com.mdhw.config.R;
import com.mdhw.system.service.CaoLiaoService;
import com.mdhw.utils.AliYunOssResult;
import com.mdhw.utils.AliYunOssServer;
import com.mdhw.utils.DateUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CaoLiaoServiceImpl implements CaoLiaoService {

    @Autowired
    private AliYunOssServer aliYunOssServer;

    public String createPrivateD4QrCode(String yqm) throws Exception
    {
        String cliD = "https://app.sjjb888.cn/#/pages/index/registerdown?yqm="+yqm;
        String cliF1 = yqm;
        String secret = "536d45c5185a5caad04dd50104e328bc";

        Map<String, Object> params = new HashMap<>();
        params.put("api_key", "CLfc0765076440aa31");
        params.put("cliT", "B623");
        params.put("cliD", cliD);
        params.put("return_file", "");
        params.put("cliF1", cliF1);


        String data = params.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("&"));
        System.out.println(data);
        data += secret;
        String sign = DigestUtils.md5Hex(data);
        System.out.println(sign);
        cliD = UriUtils.encode(cliD, StandardCharsets.UTF_8.name());
        cliF1 = UriUtils.encode(cliF1, StandardCharsets.UTF_8.name());


        //生成url请求
        String query = " https://open-api.cli.im/cli-open-platform-service/v1/labelStyle/create?cliT=B623&cliD=" + cliD +"&cliF1="+cliF1 +"&return_file=";
        System.out.println(query);
        URL url = new URL(query);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.connect();
        Date nowTime = new Date();
        InputStream stream = connection.getInputStream();
        AliYunOssResult upload = aliYunOssServer.upload(stream, "jiazhuangAppQrCode/" + DateUtil.formatDate(nowTime, DateUtil.yyyy_mm_dd_hh_mm_ss) + "__" + yqm + ".PNG");
        if(upload != null){
            return upload.getUrl();
        }
        return "";
    }


    public static void main(String[] args) throws Exception {
        String yqm = "357813";
        String cliD = "https://app.sjjb888.cn/#/pages/index/registerdown?yqm="+yqm;
        String cliF1 = yqm;
        String secret = "536d45c5185a5caad04dd50104e328bc";

        Map<String, Object> params = new HashMap<>();
        params.put("api_key", "CLfc0765076440aa31");
        params.put("cliT", "B623");
        params.put("cliD", cliD);
        params.put("return_file", "");
        params.put("cliF1", cliF1);


        String data = params.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("&"));
        System.out.println(data);
        data += secret;
        String sign = DigestUtils.md5Hex(data);
        System.out.println(sign);
        cliD = UriUtils.encode(cliD, StandardCharsets.UTF_8.name());
        cliF1 = UriUtils.encode(cliF1, StandardCharsets.UTF_8.name());


        //生成url请求
        String query = " https://open-api.cli.im/cli-open-platform-service/v1/labelStyle/create?cliT=B623&cliD=" + cliD +"&cliF1="+cliF1 +"&return_file=";
        System.out.println(query);
        URL url = new URL(query);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.connect();
        Date nowTime = new Date();
        InputStream stream = connection.getInputStream();
        /*AliYunOssResult upload = aliYunOssServer.upload(stream, "jiazhuangAppQrCode/" + DateUtil.formatDate(nowTime, DateUtil.yyyy_mm_dd_hh_mm_ss) + "__" + yqm + ".PNG");
        if(upload != null){
            return upload.getUrl();
        }
        return "";*/

        FileOutputStream out = new FileOutputStream(new File("/Users/times/Pictures/test.png"));
        IOUtils.copy(stream, out);
    }
}
